
package Dictionary;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

/**
 * This is the class which is used to create the frame that displays
 * the meaning of a selected word
 * @author SUMEDHA
 */
public class Frame extends javax.swing.JFrame {

    //Fonts which are being used in the frame are initialized
    Font fontSmall = new Font("Iskoola Pota", Font.PLAIN, 18);
    Font fontLarge = new Font("Iskoola Pota", Font.BOLD, 20);
    public String meaning, word;        //
    JTextArea displayArea = new JTextArea();

    public Frame(String meaning, String word) {
        try {
            this.meaning = meaning;
            //setting the first leter to  capital, of the word which is being selected
            this.word = capitalize(word);
            setUndecorated(true);
            //Setting the system Look and Feel
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            //initializing the frame
            initialize();
            this.setAlwaysOnTop(true);
            setLocationRelativeTo(null);
            //Stating Key & Mouse Listeners
            startEventListeners();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method initialize the Frame. Fonts are set to default 'Iskola Pota' font.
     *
     */
    private void initialize() {
        //setting the font of the display
        displayArea.setFont(fontSmall);
        //setting the background colour of the display
        displayArea.setBackground(Color.WHITE);     
        displayArea.setLineWrap(true);
        displayArea.setEditable(false);

        //Setting the border,
        Border com = new LineBorder(Color.BLUE, 1, true);
        displayArea.setBorder(new javax.swing.border.TitledBorder(com,
                " " + word + " ", 1, 2, fontLarge, Color.BLACK));

        /*Resizing the frame according to the size of the text to display.
        Lenths of the meaning string and word string is used to determnine the size*/
        int a, b;
        a = meaning.length() + word.length();
        //A small size meaning String
        if (a < 30) {
            displayArea.setSize((a) * 10, 45);
            displayArea.setText(meaning);
            this.add(displayArea);
            this.setSize(displayArea.getWidth() + 10, displayArea.getHeight() + 10);

        } //Strings are large
        else {
            b = a / 30 + 3;
            displayArea.setSize(300, b * 20);
            displayArea.setText(meaning);
            this.add(displayArea);
            this.setSize(displayArea.getWidth() + 10, displayArea.getHeight() + 10);
        }
    }

    /**
     * This method capitalize only the first letter of a given word
     * @param word        The word to be capitalized
     * @return          The word in which the first letter is  a capital
     */
    private String capitalize(String word) {
        String firstChar = String.valueOf(word.charAt(0));
        firstChar = firstChar.toUpperCase();
        String capWord = firstChar + word.substring(1, word.length());
        return capWord;
    }

    /**
     * Event listeners to catch the pressing of a key, That is frame is disposed
     * when the mouse is moved away from the frame or enter key is pressed
     */
    public class KeyListener extends KeyAdapter {

        Frame fr;

        public KeyListener(Frame fr) {
            this.fr = fr;
        }

        @Override
        public void keyPressed(KeyEvent evt) {
            if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
                fr.setVisible(false);           //Dispose the GUI
            }
        }
    }

    public class MouseListener extends MouseAdapter {

        Frame fr;

        public MouseListener(Frame fr) {
            this.fr = fr;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            fr.setVisible(false);     //Dispose the GUI
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            fr.setVisible(false);     //Dispose the GUI
        }

        @Override
        public void mouseExited(MouseEvent e) {
            fr.setVisible(false);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            fr.setVisible(false);
        }
    }

   /*
    * This method adds a Keylistener and a MouseListener to the context of
    * display area
    */
    private void startEventListeners() {
        displayArea.addKeyListener(new KeyListener(this));
        displayArea.addMouseListener(new MouseListener(this));
    }
}
